﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Petroleum.UC.Public
{
    public class DGVHelper
    {
        public static void PasteData(UIDataGridView dgvExcel)
        {
            try
            {
                string clipboardText = Clipboard.GetText(); //获取剪贴板中的内容
                if (string.IsNullOrEmpty(clipboardText))
                {
                    return;
                }
                int colnum = 0;
                int rownum = 0;
                for (int i = 0; i < clipboardText.Length; i++)
                {
                    if (clipboardText.Substring(i, 1) == "\t")
                    {
                        colnum++;
                    }
                    if (clipboardText.Substring(i, 1) == "\n")
                    {
                        rownum++;
                    }
                }

                colnum = colnum / rownum + 1;
                int selectedRowIndex, selectedColIndex;
                selectedRowIndex = dgvExcel.CurrentRow.Index;
                selectedColIndex = dgvExcel.CurrentCell.ColumnIndex;
                for (int i = 0; i < rownum; i++)
                {
                    dgvExcel.Rows.Add();
                }
                if (selectedRowIndex + rownum > dgvExcel.RowCount || selectedColIndex + colnum > dgvExcel.ColumnCount)
                {
                    MessageBox.Show("粘贴区域大小不一致");
                    return;
                }
                String[][] temp = new String[rownum][];
                for (int i = 0; i < rownum; i++)
                {
                    temp[i] = new String[colnum];
                }
                int m = 0, n = 0, len = 0;
                while (len != clipboardText.Length)
                {
                    String str = clipboardText.Substring(len, 1);
                    if (str == "\t")
                    {
                        n++;
                    }
                    else if (str == "\n")
                    {
                        m++;
                        n = 0;
                    }
                    else
                    {
                        temp[m][n] += str;
                    }
                    len++;
                }
                for (int i = selectedRowIndex; i < selectedRowIndex + rownum; i++)
                {
                    for (int j = selectedColIndex; j < selectedColIndex + colnum; j++)
                    {
                        dgvExcel.Rows[i].Cells[j].Value = temp[i - selectedRowIndex][j - selectedColIndex];
                    }
                }
            }
            catch
            {
                MessageBox.Show("粘贴区域大小不一致");
                return;
            }
        }
        public static void CopyData(DataGridView dgv)
        {
            IDataObject idataObject = Clipboard.GetDataObject();
            string[] s = idataObject.GetFormats();
            string data;
            if (!s.Any(f => f == "OEMText"))
            {
                if (!s.Any(f => f == "HTML Format"))
                {
                }
                else
                {
                    //data = idataObject.GetData("HTML Format").ToString();//多个单元格
                    data = idataObject.GetData("System.String").ToString();//多个单元格
                    copyClipboardTexttoGrid(data, dgv);
                    //msg = Message.;
                    //msg = new Message();
                    //return base.ProcessCmdKey(ref msg, Keys.Control);
                }
            }
            else
            {
                data = idataObject.GetData("OEMText").ToString();//单个单元格,处于未编辑状态
                int rowStart = dgv.SelectedCells[0].RowIndex;
                int columnStart = dgv.SelectedCells[0].ColumnIndex;
                dgv.Rows[rowStart].Cells[columnStart].Value = data;
                //return base.ProcessCmdKey(ref msg, Keys.Control);
            }
        }
        public static void CopyData(UIDataGridView dgv)
        {
            IDataObject idataObject = Clipboard.GetDataObject();
            string[] s = idataObject.GetFormats();
            string data;
            if (!s.Any(f => f == "OEMText"))
            {
                if (!s.Any(f => f == "HTML Format"))
                {
                }
                else
                {
                    //data = idataObject.GetData("HTML Format").ToString();//多个单元格
                    data = idataObject.GetData("System.String").ToString();//多个单元格
                    copyClipboardTexttoGrid(data, dgv);
                    //msg = Message.;
                    //msg = new Message();
                    //return base.ProcessCmdKey(ref msg, Keys.Control);
                }
            }
            else
            {
                data = idataObject.GetData("OEMText").ToString();//单个单元格,处于未编辑状态
                int rowStart = dgv.SelectedCells[0].RowIndex;
                int columnStart = dgv.SelectedCells[0].ColumnIndex;
                dgv.Rows[rowStart].Cells[columnStart].Value = data;
                //return base.ProcessCmdKey(ref msg, Keys.Control);
            }
        }
        private static void copyClipboardTexttoGrid(string data, UIDataGridView dgv)
        {
            string[] rows = data.Split(new string[] { "\r\n" }, StringSplitOptions.None);
            string[] cols;
            int rowStart = 0, columnStart = 0, i = 0, j = 0;
            if (dgv.SelectedCells.Count > 0)
            {
                rowStart = dgv.SelectedCells[0].RowIndex;
                columnStart = dgv.SelectedCells[0].ColumnIndex;
            }
            int count = rowStart + rows.Length - dgv.RowCount;
            if (count >= 0)
            {
                dgv.Rows.Add(count + 1);
            }
            for (i = 0; i < rows.Length && i + rowStart < dgv.RowCount; i++)
            {
                cols = rows[i].Split(new string[] { "\t" }, StringSplitOptions.None);
                for (j = 0; j < cols.Length && j + columnStart < dgv.ColumnCount; j++)
                {
                    dgv.Rows[i + rowStart].Cells[j + columnStart].Value = cols[j];
                }
            }
            dgv.ClearSelection();

            dgv.Rows[i + rowStart - 1].Cells[j + columnStart - 1].Selected = true;
        }
        private static void copyClipboardTexttoGrid(string data,DataGridView dgv)
        {
            string[] rows = data.Split(new string[] { "\r\n" }, StringSplitOptions.None);
            string[] cols;
            int rowStart = 0, columnStart = 0, i = 0, j = 0;
            if (dgv.SelectedCells.Count > 0)
            {
                rowStart = dgv.SelectedCells[0].RowIndex;
                columnStart = dgv.SelectedCells[0].ColumnIndex;
            }
            int count = rowStart + rows.Length - dgv.RowCount;
            if (count >= 0)
            {
                dgv.Rows.Add(count + 1);
            }
            for (i = 0; i < rows.Length && i + rowStart < dgv.RowCount; i++)
            {
                cols = rows[i].Split(new string[] { "\t" }, StringSplitOptions.None);
                for (j = 0; j < cols.Length && j + columnStart < dgv.ColumnCount; j++)
                {
                    dgv.Rows[i + rowStart].Cells[j + columnStart].Value = cols[j];
                }
            }
            dgv.ClearSelection();

            dgv.Rows[i + rowStart - 1].Cells[j + columnStart - 1].Selected = true;
        }
    }
}
